﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Engine.SceneControl.Scene
{
    public sealed class QuadRenderer
    {
        private VertexPositionTexture[] verts;         
        private VertexDeclaration VertexDeclaration { get; set; }
        private Effect effect;
        
        public void LoadContent(EngineStuff engine,String effectName) 
        {
            GraphicsDevice myDevice = engine.GraphicsDevice;

            if (effectName != null)
            {
                effect = engine.Content.Load<Effect>(effectName);
            }
            else
            {
                effect = null;
            }
                

            verts = new[] {
                new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)), 
                new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)), 
                new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)), 
                new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0))
            }; 
            VertexDeclaration = new VertexDeclaration(myDevice, VertexPositionTexture.VertexElements); 
            
        }
        public EffectParameterCollection Parameter
        {
            get
            {
                return effect.Parameters;
            }
        }

        public void DrawQuad(GraphicsDevice myDevice,Texture2D tex)
        {
            if(effect != null)
            {
            myDevice.Textures[0] = tex;
            effect.Begin();            
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            { 
                pass.Begin(); 
                myDevice.VertexDeclaration = VertexDeclaration; 
                myDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, verts, 0, 2); 
                pass.End(); 
            } 
            effect.End(); 
            }
            else
            {
                GraphicsDevice device = myDevice;
                
            //device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);
            using (SpriteBatch sprite = new SpriteBatch(device))
            {
                sprite.Begin(SpriteBlendMode.None, SpriteSortMode.Texture, SaveStateMode.SaveState);
                sprite.Draw(tex, new Vector2(0, 0), null, Color.White, 0, new Vector2(0, 0), 1, SpriteEffects.None,1);
                sprite.End();
            }
            }
         
        }   

    
    }
}
